/* @(#)GSAfficheProcessor.java    V0.1
 *
 * Project: NEWGS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   owen         2006-5-24
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.business;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.struts.upload.FormFile;

import com.hitrust.gs.bean.GSAfficheBean;
import com.hitrust.gs.dao.GSAfficheDAO;
import com.hitrust.gs.form.GSAfficheForm;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseProcesser;
import com.hitrust.gs.util.PubTool;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;

/**
 * 
 */
public class GSAfficheProcessor implements GSBaseProcesser{
    /**
     * 增加公告信息任务
     * @param aGSBalanceFeeBean
     * @throws GSException
     */
     public static void addTask(GSAfficheBean aGSAfficheBean)throws GSException {
         GSAfficheDAO tGSAfficheDAO = new GSAfficheDAO();
            
             if (tGSAfficheDAO.add(aGSAfficheBean) == false) {
                 throw new GSException("增加公告信息失败!");
             }
         }
     
     /**
      * 修改公告信息任务
      * @param aGSBalanceFeeBean
      * @throws GSException
      */
     public static void modifyTask(GSAfficheBean aGSAfficheBean)throws GSException {
         GSAfficheDAO tGSAfficheDAO = new GSAfficheDAO();
            
             if (tGSAfficheDAO.modify(aGSAfficheBean) == false) {
                 throw new GSException("修改公告信息失败!");
             }
         }
    
     /**
      * 得到所有的公告信息
      * @return
      * @throws GSException
      */
     public static ArrayList getAllAffiche()throws GSException{
         GSAfficheDAO tGSAfficheDAO = new GSAfficheDAO();
         
         ArrayList tArrayList=new ArrayList();
         try {
             ArrayList  tAfficheList=tGSAfficheDAO.queryAllAffiche();
             if (tAfficheList.size() > 0) {
                 
                 for (int i = 0; i < tAfficheList.size(); i++) {

                     GSAfficheBean tGSAfficheBean = (GSAfficheBean) tAfficheList.get(i);
                     String[] tStr = new String[4];
                     tStr[0] = tGSAfficheBean.getGs_affiche_id();
                     tStr[1] = tGSAfficheBean.getGs_affiche_title();
                     tStr[2] = tGSAfficheBean.getGs_affiche_filename();
                     tStr[3] = tGSAfficheBean.getGs_affiche_date();
                     
                     tArrayList.add(tStr);
                     
                 }
             }
             
         } catch (GSException e) {
             GSLogWriter.error("GSAfficheProcessor GSException: " +e.toString());
         }
         return tArrayList;
     }
     
     /**
      * 得到某个公告信息的详细信息
      * 
      * @param aAfficheId
      * 
      * @throws GSException          
      */
     public static GSAfficheBean getOneAffiche(String aAfficheId)throws GSException{
         GSAfficheDAO tGSAfficheDAO = new GSAfficheDAO();
         
         GSAfficheBean tGSAfficheBean = new GSAfficheBean();  
         try {
             //通过公告编号
             tGSAfficheBean=tGSAfficheDAO.getAffiche(aAfficheId);
         } catch (GSException e) {
             GSLogWriter.error("GSAfficheProcessor GSException: " +e.toString());
         }
         return tGSAfficheBean;
     }
     /**
      * 删除公告信息
      * @param aGSBalanceFeeBean
      * @throws GSException
      */
     public static void delTask(String aAfficheId)throws GSException {
          GSAfficheDAO tGSAfficheDAO = new GSAfficheDAO();
            
             if (tGSAfficheDAO.delete(aAfficheId) == false) {
                 throw new GSException("删除公告信息失败!");
             }
         }
     
     /**
      * 通过某个条件得到所有的公告信息
      * @return
      * @throws GSException
      */
     public static ArrayList getOtherAffiche(GSAfficheForm aGSAfficheForm)throws GSException{
         GSAfficheDAO tGSAfficheDAO = new GSAfficheDAO();
         
         ArrayList tArrayList=new ArrayList();
         try {
             String[] tDateStr = new String[2];
             // 取得查询参数订单创建最小日期和最大日期
             tDateStr[0] = aGSAfficheForm.getGs_start_date();
             tDateStr[1] = aGSAfficheForm.getGs_end_date();
             // 格式化日期
             tDateStr = DateUtil.formateDate(tDateStr);
             aGSAfficheForm.setGs_start_date(tDateStr[0]);
             aGSAfficheForm.setGs_end_date(tDateStr[1]);
             
             ArrayList  tAfficheList=tGSAfficheDAO.queryOtherAffiche(aGSAfficheForm);
             if (tAfficheList.size() > 0) {
                 
                 for (int i = 0; i < tAfficheList.size(); i++) {

                     GSAfficheBean tGSAfficheBean = (GSAfficheBean) tAfficheList.get(i);
                     String[] tStr = new String[4];
                     tStr[0] = tGSAfficheBean.getGs_affiche_id();
                     tStr[1] = tGSAfficheBean.getGs_affiche_title();
                     tStr[2] = tGSAfficheBean.getGs_affiche_filename();
                     tStr[3] = tGSAfficheBean.getGs_affiche_date();
                     
                     tArrayList.add(tStr);
                     
                 }
             }
             
         } catch (GSException e) {
             //GSLogWriter.error(this,e.toString());
             throw new GSException(GSException.E19999,GSException.E19999_MSG);
         }
         return tArrayList;
     }
     
     public static void saveFile(FormFile aFile,String aPath,String aSaveName) throws GSException {

         File file = null;
         FileOutputStream fos = null;
         try{
             if (aFile.getFileSize() == 0) {
                      throw new GSException("当前文件不存在或为空");
                  }
             if (aFile.getFileSize() > 3145728 ){
                      throw new GSException("上传文件不能超过3M");
                  }
             else{
                
                 String realFilePath = PubTool.apendSepa(aPath) + aSaveName;
                 //String filename = "/upload/"+aSaveName;
                 file = new File(realFilePath);
                 fos = new FileOutputStream(file);
                 InputStream stream = aFile.getInputStream();// 把文件传入
                 
                 int bytesRead = 0;
                 byte[] buffer = new byte[8192];
                 while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                     fos.write(buffer, 0, bytesRead);// 文件传入服务器
                 } 
                 fos.close();
                 stream.close();
             }
         }catch(IOException ioe){
             GSLogWriter.error("----" + ioe.getMessage());             
             ioe.printStackTrace();
             throw new GSException (GSException.E17793,GSException.E17793_MSG);
         }
     }
     
     public static void saveFileByRealPath(FormFile aFile,String aPath,String aSaveName) throws GSException {

         File file = null;
         FileOutputStream fos = null;
         try{
             if (aFile.getFileSize() == 0) {
                      throw new GSException("当前文件不存在或为空");
                  }
             if (aFile.getFileSize() > 3145728 ){
                      throw new GSException("上传文件不能超过3M");
                  }
             else{
                
                 String realFilePath = aPath+"\\" + aSaveName;
                 //String filename = "/upload/"+aSaveName;
                 file = new File(realFilePath);
                 fos = new FileOutputStream(file);
                 fos.write(aFile.getFileData());

                 fos.close();

             }
         }catch(IOException ioe){
             GSLogWriter.error("----" + ioe.getMessage());             
             ioe.printStackTrace();
             throw new GSException (GSException.E17793,GSException.E17793_MSG);
         }
     }
}
